package com.lingo.a2b.common.loader;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lingo
 * @date 2020/5/22 12:46
 * @Company: www.xyb2b.com
 */
@Component
public class DynamicClassLoader extends ClassLoader {

    public Map<String,byte[]> classNameToBytes = new HashMap<>();

    @Override
    public Class<?> findClass(String name) {
        Class<?> aClass = findLoadedClass(name);
        if (aClass == null){
            byte[] bytes = classNameToBytes.get(name);
            if (bytes != null){
                aClass = defineClass(null, bytes,0,bytes.length);
            }
        }
        return aClass;
    }

    public Class<?> defineClass(byte[] bytes){
        return this.defineClass(null,bytes,0,bytes.length);
    }
}
